package com.m3958.firstgwt.server.dao;

import java.util.Date;

import nl.captcha.Captcha;

import com.m3958.firstgwt.client.types.ServerErrorCode;
import com.m3958.firstgwt.server.accesschecker.XinJianChecker;
import com.m3958.firstgwt.server.model.XinJian;
import com.m3958.firstgwt.server.model.XinJianCat;
import com.m3958.firstgwt.server.session.ErrorMessages;
import com.m3958.firstgwt.server.types.ObjectOperation;
import com.m3958.firstgwt.server.types.SmartError;
import com.m3958.firstgwt.shared.type.SmartConstants;


public class XinJianChangeStrategy  extends BaseModelChangeStrategy implements ModelChangeStrategy<XinJian> {

	@Override
	public boolean extraPersistTask(XinJian model) {
		XinJianCat xjc = emp.get().find(XinJianCat.class, getReqPs().getRelationModelId());
		if(xjc == null)return false;
		checkCaptcha();
		model.setAudit(false);
		return true;
	}
	
	private boolean checkCaptcha(){
		if(getSu().isLogined()){
			getReqPs().getReq().getSession().removeAttribute(Captcha.NAME);
			return true;
		}
		Captcha captcha = (Captcha) getReqPs().getReq().getSession().getAttribute(Captcha.NAME);
		String answer = getReqPs().getStringValue("answer");
		if (captcha == null || !captcha.isCorrect(answer)){
			getErrors().addError(new SmartError("验证码错误！", ServerErrorCode.CAPTCHA_ERROR));
			return false;
		}else{
			getReqPs().getReq().getSession().removeAttribute(Captcha.NAME);
			return true;
		}
	}

	private boolean setInitCat(XinJian model) {
		if(getReqPs() != null && getReqPs().getRelationModelId() != SmartConstants.NONE_EXIST_MODEL_ID){
			XinJianCat s = emp.get().find(XinJianCat.class, getReqPs().getRelationModelId());
			s.manageRelation(model, true, "", injector.getInstance(ErrorMessages.class));
			model.getXjCats().add(s);
			model.setSiteId(s.getSiteId());
			return true;
		}else{
			return false;
		}
	}


	@Override
	public boolean extraUpdateTask(XinJian oldModel,XinJian newModel) {
		if(newModel.getRepliedAt() == null)newModel.setRepliedAt(new Date());
		newModel.setRepliedAt(afterAddHourMinutes(newModel.getRepliedAt()));
		attachAttachments(oldModel);
		if(oldModel.isAudit() != newModel.isAudit() && !canAudit(oldModel,newModel)){
			newModel.setAudit(oldModel.isAudit());
		}
		return true;
	}
	
	private boolean canAudit(XinJian oldModel,XinJian newModel) {
		if(getRso().isSiteEditor(oldModel.getSiteId()) || getRso().isSiteOwner(oldModel.getSiteId()))return true;
		XinJianChecker ac = injector.getInstance(XinJianChecker.class);
		for(XinJianCat s : oldModel.getXjCats()){
			if(ac.hasTreeContainerPerms(XinJianCat.class.getName(), s.getId(), ObjectOperation.AUDIT_CONTENT)){
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean extraRemoveTask(XinJian model) {
//		addCacheBreakerItem(getCbi(model, ObAction.DELETE));
		return true;
	}

	@Override
	public boolean afterPersist(XinJian model) {
		setInitCat(model);
		attachAttachments(model);
//		addCacheBreakerItem(getCbi(model, ObAction.ADD));
		return false;
	}
	
//	private CacheBreakerItem getCbi(XinJian article,ObAction action){
//		CacheBreakerItem cbi = new CacheBreakerItem();
//		cbi.setAction(action);
//		cbi.setCreatedAt(new Date());
//		cbi.setDone(false);
//		cbi.setObId(article.getId());
//		cbi.setObName("");
//		cbi.setObType(ObType.ARTICLE);
//		return cbi;
//	}
	
	private void attachAttachments(XinJian model) {
//		model.getAttachments().clear();
//		String at = getReqPs().getStringValue(SmartParameterName.ATTACHMENT_JSON);
//		if(at == null)return;
//		JSONArray ja = JSONArray.fromObject(at);
//		JSONArray jja;
//		Integer oid;
//		Asset a;
//		for(int i = 0;i < ja.size();i++){
//			JSONObject jo = (JSONObject) ja.get(i);
//			if(jo.containsKey(AttachmentBox.class.getName())){
//				jja = jo.getJSONArray(AttachmentBox.class.getName());
//				for(int j = 0;j<jja.size();j++){
//					oid = jja.getJSONObject(j).getInt(CommonField.ID.getValue());
//					a = emp.get().find(Asset.class, oid);
//					model.addAttachment(a);
//				}
//			}
//		}
	}
	
}
